<?php  if (!defined('PATH_HD')) exit('No direct script access allowed');

class IndexControl extends Control{
    function index(){
        if(!isset($_SESSION['userid'])){
            $this->error('您未登录,请先登录');
            die;
        }else{
            $db=M('user');
            $userinfo=$db->find($_SESSION['userid']);
        }
        $this->assign('userinfo',$userinfo);
        $this->display();
    }
    // 会员修改个人账号显示界面
    function changeuser(){
        $db=M('user');
        $userinfo=$db->find($_SESSION['userid']);
        $this->assign('userinfo',$userinfo);
    	$this->display();
    }
    // 执行修改信息
    function change(){
        $db=M('user');
        $username=$_SESSION['username'];
        $password=md5($_POST['passworder']);
        $email=$_POST['email'];
        $userid=$_SESSION['userid'];
        $data=array('userid'=>$userid,'password'=>$password,'email'=>$email);
        if($username){
            if($user = $db->find("username='$username'")){
                if($user['password'] ==md5($_POST['password'])){
                    if($db->save($data)){
                        $this->success("密码修改成功","index");
                    }
                }else{
                    $this->error("原密码错误","login");
                }
            }else{
                $this->error("用户不存在");
            }
        }
    }
    //会员添加文章显示视图
    function addshow(){
    	if (!isset($_GET['cid'])) {
            $this->assignCategoryData();
        }
    	$this->display();
    }
    // 执行添加文章
    function addarc(){
    	$data = $_POST;
        $data['addtime'] = $_SERVER['REQUEST_TIME'];
        $data['thumbimg'] = isset($data['thumbimg'][1]) ? $data['thumbimg'][1]['path'] : ''; //缩略图
        $data['shenhe']=0;
        $db = M("arc");
        if ($db->add($data)) {
            $this->success("发表文章成功", "index");
        } else {
            $this->error("文章添加失败");
        }
    }
    //向模板分配栏目
    private function assignCategoryData() {
        $db = M("category");
        $result = $db->all();
        $data = Data::channel($result, "cid", "pid", 0, "", 2, "--");
        $this->assign("category", $data);
    }
    //退出登录
    function quit(){
        session_unset();
        session_destroy();
        go('index/index');
    }
}
?>